<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Surat_jaminan extends CI_Controller {

	function __construct() {
		parent::__construct();

		//jika digunakan di mayoritas function, panggil disini
		//model
		$this->load->model('m_surat_jaminan');
		//helper tanggal
		$this->load->helper('id_date');
		//library breadcrum/untuk navigasi
		$this->load->library(array('breadcrumb','form_validation'));
	}

	function index() {

		//rule type pada function ini: Read
		$this->rule->type('R');

		//Layout
		//title
		$this->layout->set_title('Data Surat Jaminan');

		//Hapus Session
		$this->session->unset_userdata('keyword');
		$this->session->unset_userdata('tgl_awal');
		$this->session->unset_userdata('tgl_akhir');
		$this->session->unset_userdata('jenis_kunjungan');
		$this->session->unset_userdata('status');

		//meta description jika perlu
		$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

		//css tambahan bila perlu
		$this->layout->set_include('css', 'themes/back/css/datatables/dataTables.bootstrap.css');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/jquery-ui.js', 'header');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/datepicker-id.js', 'header');
		//css tambahan
		$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');
		$this->layout->set_include('css', 'themes/back/bundle/datepicker/datepicker.css');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Surat');	
		$this->breadcrumb->add_crumb('Surat Jaminan');		

		//paging dan ambil data dari model
		$this->load->library('paging');                         		//Load library "paging"
    $config['sql']      				= $this->m_surat_jaminan->get();//Query SQL - bukan array lho ya...
    $config['per_page'] 				= 15;                            //jumlah item tiap halaman
    $data = $this->paging->create_pagination($config);   				//Load fungsi di library "paging" untuk membuat pagination. 

    	//judul besar
		$data['primary_title'] 			= '<i class="ion-android-note"></i> Surat';
		$data['sub_primary_title'] 	= '';
		$data['jenis_kunjungan']		= $this->m_surat_jaminan->get_jenis_kunjungan();

		//menghilangka session jika ada
		$this->session->unset_userdata('keyword');

		//menggunakan theme back
		$this->layout->theme('back','surat_jaminan/index', $data);
	}

	function cari() {

		//rule type pada function ini: Read
		$this->rule->type('R');

		//Layout
		//title
		$this->layout->set_title('Cari Data Surat Jaminan');

		//meta description jika perlu
		$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

		//css tambahan bila perlu
		$this->layout->set_include('css', 'themes/back/css/datatables/dataTables.bootstrap.css');

		//js tambahan jika perlu
		$this->layout->set_include('js', 'themes/back/js/plugins/datatables/jquery.dataTables.js');
		$this->layout->set_include('js', 'themes/back/js/plugins/datatables/dataTables.bootstrap.js');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/jquery-ui.js', 'header');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/datepicker-id.js', 'header');
		//css tambahan
		$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');
		$this->layout->set_include('css', 'themes/back/bundle/datepicker/datepicker.css');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Surat');	
		$this->breadcrumb->add_crumb('Surat Jaminan');	

		//buat cari
		if(isset($_POST['keyword'])){
			$data['keyword'] 					= $this->input->post('keyword');
			$data['tgl_awal']					= $this->input->post('tgl_awal');
			$data['tgl_akhir']				= $this->input->post('tgl_akhir');
			$data['jenis_kunjungan']	= $this->input->post('jenis_kunjungan');
			$data['status']						= $this->input->post('status');
			$this->session->set_userdata('keyword', $data['keyword']);
			$this->session->set_userdata('tgl_awal', $data['tgl_awal']);
			$this->session->set_userdata('tgl_akhir', $data['tgl_akhir']);
			$this->session->set_userdata('jenis_kunjungan', $data['jenis_kunjungan']);
			$this->session->set_userdata('status', $data['status']);
		} else {
			$data['keyword'] 					= $this->session->userdata('keyword');
			$data['tgl_awal'] 				= $this->session->userdata('tgl_awal');
			$data['tgl_akhir'] 				= $this->session->userdata('tgl_akhir');
			$data['jenis_kunjungan'] 	= $this->session->userdata('jenis_kunjungan');
			$data['status'] 					= $this->session->userdata('status');
		}	

		//paging dan ambil data dari model
		$this->load->library('paging');  
		if($data['tgl_awal'] != '' || $data['tgl_akhir'] != '') {                      								//Load the "paging" library
    	$config['sql']      				= $this->m_surat_jaminan->get_cari_tanggal($data['keyword'],
    																$data['jenis_kunjungan'], $data['status'], 
    																iddate2dbdate($data['tgl_awal']), 
    																iddate2dbdate($data['tgl_akhir']));	//your SQL - bukan array lho ya...
    }else{
    	$config['sql']      				= $this->m_surat_jaminan->get_cari($data['keyword'],
    																$data['jenis_kunjungan'], $data['status']);
    }
    $config['per_page'] 				= 15;                            																	//Display items per page
    $data = $this->paging->create_pagination($config);   																					//Load function in "paging" libraryfor create pagination. 

    //judul besar
		$data['primary_title'] 			= '<i class="ion-android-note"></i> Surat';
		$data['sub_primary_title'] 	= '';
		$data['jenis_kunjungan']		= $this->m_surat_jaminan->get_jenis_kunjungan();

		//menggunakan theme back
		$this->layout->theme('back','surat_jaminan/index', $data);
	}

	function detail($kd_klaim = '') {

		//rule type pada function ini: Read
		$this->rule->type('R');

		//parameter yang dipelukan query
		$params = array($kd_klaim);

		//Layout
		//title
		$this->layout->set_title('Detail Surat Jaminan');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Surat');
		$this->breadcrumb->add_crumb('Jaminan', site_url('surat_jaminan'));
		$this->breadcrumb->add_crumb('Detail Surat Jaminan');

		//judul besar
		$data['primary_title'] = '<i class="ion-android-note"></i> Surat';
		$data['sub_primary_title'] = '';

		//ambil data dari model dengan berdasarkan parameter diatas
		$data['list'] 			= $this->m_surat_jaminan->get_by($params);
		$data['diagnosa']		= $this->m_surat_jaminan->get_diagnosa_by($data['list']['kd_surat_jaminan']);
		$data['lampiran']		= $this->m_surat_jaminan->get_lampiran_by($data['list']['kd_surat_jaminan']);
		
		//menggunakan theme back
		$this->layout->theme('back','surat_jaminan/detail', $data);
	}

	function cetak($kd_klaim = '') {

		//rule type pada function ini: Read
		$this->rule->type('R');

		//parameter yang dipelukan query
		$params = array($kd_klaim);

		//Layout
		//title
		$this->layout->set_title('Detail Surat Jaminan');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Surat');
		$this->breadcrumb->add_crumb('Jaminan', site_url('surat_jaminan'));
		$this->breadcrumb->add_crumb('Detail Surat Jaminan');

		//judul besar
		$data['primary_title'] = '<i class="ion-android-note"></i> Surat';
		$data['sub_primary_title'] = '';

		//ambil data dari model dengan berdasarkan parameter diatas
		$data['list'] 			= $this->m_surat_jaminan->get_by($params);
		$data['diagnosa']		= $this->m_surat_jaminan->get_diagnosa_by($data['list']['kd_surat_jaminan']);
		$data['lampiran']		= $this->m_surat_jaminan->get_lampiran_by($data['list']['kd_surat_jaminan']);

		//menggunakan theme back
		$this->load->view('back/layouts/surat_jaminan/cetak', $data);
	}

	function add() {

		//rule type pada function ini: Create
		$this->rule->type('C');

		//Layout
		//title
		$this->layout->set_title('Tambah Surat Jaminan');

		//js tambahan
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/jquery-ui.js', 'header');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/datepicker-id.js', 'header');
		//css tambahan
		$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');
		$this->layout->set_include('css', 'themes/back/bundle/datepicker/datepicker.css');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Surat');
		$this->breadcrumb->add_crumb('Jaminan', site_url('surat_jaminan'));
		$this->breadcrumb->add_crumb('Tambah Agama');

		//judul besar
		$data['primary_title'] = 'Surat';
		$data['sub_primary_title'] = '';

		$data['ppk']							= $this->m_surat_jaminan->get_ppk();
		$data['jenis_kunjungan']	= $this->m_surat_jaminan->get_jenis_kunjungan();
		$data['kelas_perawatan']	= $this->m_surat_jaminan->get_kelas_perawatan();
		$data['diagnosa']					= $this->m_surat_jaminan->get_diagnosa();
		$data['ppk_rujukan']			= $this->m_surat_jaminan->get_rujukan();

		//menggunakan theme back
		$this->layout->theme('back','surat_jaminan/add', $data);
	}

	function save() {
		//rule type pada function ini: Create
		$this->rule->type('C');
		if($this->input->post('kd_peserta') != ''){
			$jenis_kunjungan 	= $this->input->post('jenis_kunjungan');
			$date 						= date('my');
			$kode 						= $date."".sprintf("%02s", $jenis_kunjungan);
			
			$kd_max 					= $this->m_surat_jaminan->get_kode_max($kode);
			$noUrut 					= (int) substr($kd_max, 6, 4);
			$noUrut++;
			$kd_jaminan				= $kode.sprintf("%04s", $noUrut);

			$nomer_max 				= $this->m_surat_jaminan->get_nomer_max($jenis_kunjungan);
			$noUrut 					= (int) substr($nomer_max, 0, 4);
			$noUrut++;
			$nomer_jaminan		= sprintf("%04s", $noUrut);

			$dataInsert = array(
				'kd_surat_jaminan'		=> $kd_jaminan,
				'kd_peserta' 					=> $this->input->post('kd_peserta'),
				'nomer_surat'					=> $nomer_jaminan,
				'id_jenis_kunjungan'	=> $this->input->post('jenis_kunjungan'),
				'kd_ppk'							=> $this->input->post('ppk'),
				'id_kelas'						=> $this->input->post('kelas'),
				'tanggal_input'				=> date("Y-m-d h:i:s"),
				'kd_rujukan'					=> $this->input->post('kd_rujukan'),
				'keterangan'					=> $this->input->post('keterangan'),
				'diagnosa_lain'				=> $this->input->post('diagnosa_lain')
			);

			if(!$this->m_surat_jaminan->save_surat_jaminan($dataInsert)){
				$this->session->set_flashdata('gagal', 'Data surat jaminan gagal ditambahkan.');
				redirect('surat_jaminan','refresh');
			}else{
					//Diagnosa
					$diagnosa 				= $this->input->post('diagnosa');
					$i = 0;
					foreach ($diagnosa as $value) {
						$data_diagnosa = array(
							'kd_surat_jaminan' 	=> $kd_jaminan, 
							'kd_diagnosa'				=> $value
						);
						if($value != ''){
							$this->m_surat_jaminan->save_diagnosa($data_diagnosa);
						}
						$i++;
					}

					//Membuat kode klaim
					$tgl 							= date('my');
					$klaim_max 				= $this->m_surat_jaminan->get_klaim_max($tgl);
					$noUrut 					= (int) substr($klaim_max, 4, 4);
					$noUrut++;
					$kd_klaim					= $tgl."".sprintf("%04s", $noUrut);
					
					$klaimInsert = array(
						'kd_klaim'						=> $kd_klaim,
						'kd_surat_jaminan'		=> $kd_jaminan,
						'jenis'								=> 'baru',
						'id_petugas_loket'		=> user_id(),
						'tgl_surat'						=> iddate2dbdate($this->input->post('tanggal')),
					);
					$this->m_surat_jaminan->save_klaim($klaimInsert);

					$config['upload_path'] 		= './uploads/lampiran/';
					$config['allowed_types'] 	= 'gif|jpg|png';
					$this->load->library('upload', $config);
					// Looping $_FILES dan membuat array baru
					$jml_file = count($_FILES['lampiran']);
					foreach($_FILES['lampiran'] as $key=>$val){
						$i = 1;
						foreach($val as $v){
							$field_name = "file_".$i;
							$_FILES[$field_name][$key] = $v;
							$i++;
						}
					}
					// menghapus array awal, karena kita sudah memiliki array baru
					unset($_FILES['lampiran']);

					//variabel error diubah, dari string menjadi array
					$error = array();
					$success = array();
					for($j=1;$j<=$jml_file;$j++){
						//echo 'fileku '.$field_name."<br>";
						if ($this->upload->do_upload("file_".$j)){
							$image_data 	= $this->upload->data();
							$nama_file		= $image_data['file_name'];
							//echo $nama_file."<br>";
							$arr_data_img	= array(
								'kd_surat_jaminan' 	=> $kd_jaminan,
								'nama_file'					=> $nama_file
							);
							$this->m_surat_jaminan->save_lampiran($arr_data_img);
						}
					}
				$this->session->set_flashdata('sukses', 'Data surat jaminan berhasil ditambahkan.');
				redirect("surat_jaminan/detail/$kd_jaminan",'refresh');
			}
		}else{
			$this->session->set_flashdata('gagal', 'Data surat jaminan gagal ditambahkan.');
			redirect('surat_jaminan');
		}
	}

	function edit($kd_jaminan = '') {

		//rule type pada function ini: Update
		$this->rule->type('U');

		//parameter yang dipelukan query
		$params = array($kd_jaminan);

		//layout
		//title
		$this->layout->set_title('Edit Surat Jaminan');

		//js tambahan
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/jquery-ui.js', 'header');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/datepicker-id.js', 'header');
		//css tambahan
		$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');
		$this->layout->set_include('css', 'themes/back/bundle/datepicker/datepicker.css');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Surat');
		$this->breadcrumb->add_crumb('Jaminan', site_url('surat_jaminan'));
		$this->breadcrumb->add_crumb('Edit Jaminan');

		//judul besar
		$data['primary_title'] = 'Surat';
		$data['sub_primary_title'] = '';

		//ambil data dari model dengan berdasarkan parameter diatas
		$data['list'] 						= $this->m_surat_jaminan->get_by($params);
		$data['ppk']							= $this->m_surat_jaminan->get_ppk();
		$data['jenis_kunjungan']	= $this->m_surat_jaminan->get_jenis_kunjungan();
		$data['kelas_perawatan']	= $this->m_surat_jaminan->get_kelas_perawatan();
		$data['diagnosa']					= $this->m_surat_jaminan->get_diagnosa();
		$data['ppk_rujukan']			= $this->m_surat_jaminan->get_rujukan();
		$data['diagnosa_jaminan']	= $this->m_surat_jaminan->get_diagnosa_by($params);
		$data['lampiran_jaminan']	= $this->m_surat_jaminan->get_diagnosa_by($params);

		//menggunakan theme back
		$this->layout->theme('back','surat_jaminan/edit', $data);
	}

	function update() {

		//rule type pada function ini: Update
		$this->rule->type('U');
		//ambil data dari form
		
		$kd_jaminan = $this->input->post('kd_surat_jaminan');
		$dataUpdate = array(
			'kd_ppk' 				=> $this->input->post('ppk'),
			'id_kelas'			=> $this->input->post('kelas'),
			'kd_rujukan'		=> $this->input->post('kd_rujukan'),
			'keterangan'		=> $this->input->post('keterangan'),
			'diagnosa_lain'	=> $this->input->post('diagnosa_lain'),
			);

		if(!$this->m_surat_jaminan->update($kd_jaminan, $dataUpdate)){
			$this->session->set_flashdata('gagal', 'Data <b>'.$this->input->post('nama').'</b> gagal diperbaharui.');
			redirect($this->input->post('redirurl'),'refresh');
		}else{
			// $jml_diagnosa = count($this->input->post('diagnosa'));
			// if($jml_diagnosa > 0){
			// 	$diagnosa 				= $this->input->post('diagnosa');
			// 	$this->m_surat_jaminan->delete_diagnosa($kd_jaminan);
			// 	$i = 0;
			// 	foreach ($diagnosa as $value) {
			// 		$data_diagnosa = array(
			// 			'kd_surat_jaminan' 	=> $kd_jaminan, 
			// 			'kd_diagnosa'				=> $value
			// 		);
			// 		if($value != ''){
			// 			$this->m_surat_jaminan->save_diagnosa($data_diagnosa);
			// 		}
			// 		$i++;
			// 	}
			// }

			//Membuat kode klaim	
			$kd_klaim	= $this->input->post('kd_klaim');		
			$klaimUpdate = array(
				'tgl_surat'						=> iddate2dbdate($this->input->post('tanggal')),
			);
			$this->m_surat_jaminan->update_klaim($kd_klaim,$klaimUpdate);

			$config['upload_path'] 		= './uploads/lampiran/';
			$config['allowed_types'] 	= 'gif|jpg|png';
			$this->load->library('upload', $config);
			// Looping $_FILES dan membuat array baru
			$jml_file = count($_FILES['lampiran']);
			foreach($_FILES['lampiran'] as $key=>$val){
				$i = 1;
				foreach($val as $v){
					$field_name = "file_".$i;
					$_FILES[$field_name][$key] = $v;
					$i++;
				}
			}
			// menghapus array awal, karena kita sudah memiliki array baru
			unset($_FILES['lampiran']);

			//variabel error diubah, dari string menjadi array
			$error = array();
			$success = array();
			for($j=1;$j<=$jml_file;$j++){
				//echo 'fileku '.$field_name."<br>";
				if ($this->upload->do_upload("file_".$j)){
					$image_data 	= $this->upload->data();
					$nama_file		= $image_data['file_name'];
					//echo $nama_file."<br>";
					$arr_data_img	= array(
						'kd_surat_jaminan' 	=> $kd_jaminan,
						'nama_file'					=> $nama_file
					);
					$this->m_surat_jaminan->save_lampiran($arr_data_img);
				}
			}
			$this->session->set_flashdata('sukses', 'Data <b>'.$this->input->post('nama').'</b> berhasil diperbaharui.');
			redirect($this->input->post('redirurl'),'refresh');
		}
	}

	function delete($kd_jaminan = '') {

		//rule type pada function ini: Delete
		$this->rule->type('D');
		$cek = $this->m_surat_jaminan->cek_jaminan($kd_jaminan);
		if($cek == 0){
			$this->m_surat_jaminan->delete_diagnosa($kd_jaminan);
			$this->m_surat_jaminan->delete_lampiran($kd_jaminan);
			$this->m_surat_jaminan->delete_klaim($kd_jaminan);
			if(!$this->m_surat_jaminan->delete($kd_jaminan)){
				$this->session->set_flashdata('gagal', 'Data gagal dihapus.');
				redirect($_SERVER['HTTP_REFERER'],'refresh');
			}else{
				$this->session->set_flashdata('sukses', 'Data berhasil dihapus.');
				redirect($_SERVER['HTTP_REFERER'],'refresh');
			}
		}else{
			$this->session->set_flashdata('sukses', 'Data sudah digunakan untuk klaim, tidak dapat dihapus!.');
			redirect($_SERVER['HTTP_REFERER'],'refresh');
		}
	}

	

	function excel(){
		$this->rule->type('R');
		//load our new PHPExcel library
		$this->load->library('excel');
		//activate worksheet number 1
		$this->excel->setActiveSheetIndex(0);
		//name the worksheet
		$this->excel->getActiveSheet()->setTitle('Data Agama');
		//set cell A1 content with some text
		$this->excel->getActiveSheet()->setCellValue('A1', 'Data Surat Jaminan');
		$this->excel->getActiveSheet()->setCellValue('A3', 'No.');
		$this->excel->getActiveSheet()->setCellValue('B3', 'Agama');
		//change the font size
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(11);
		//make the font become bold
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		//merge cell A1 until D1
		$this->excel->getActiveSheet()->mergeCells('A1:D1');
		//set aligment to center for that merged cell (A1 to D1)
		$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		//set style
	  $TitleStyle = array(
	    'borders' => array(
        'allborders' => array(
            'style' => PHPExcel_Style_Border::BORDER_THIN
        )
	    ),
	    'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => '00994C') // hijau tua
      ),
      'font'  => array(
	      'bold'  => false,
	      'color' => array('rgb' => 'FFFFFF'),
	      'size'  => 15,
	      'name'  => 'Verdana'
  		)
  	);

	  $HeaderStyle = array(
	    'borders' => array(
        'allborders' => array(
            'style' => PHPExcel_Style_Border::BORDER_THIN
        )
	    ),
	    'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => '32CD32') // hijau
      )
  	);

  	$ContentStyle = array(
	    'borders' => array(
        'allborders' => array(
            'style' => PHPExcel_Style_Border::BORDER_THIN
        )
	    ),
	    'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'FFFF00') // kuning
      )
  	);
  	
		$this->excel->getActiveSheet()->getStyle('A1:D1')->applyFromArray($TitleStyle);
		$this->excel->getActiveSheet()->getStyle('A2:B2')->applyFromArray($HeaderStyle);
		//isi
		$list_data = $this->m_master_agama->get_export();
		$cols = 3;
		$num = 1;
		foreach($list_data as $row){
			$col = $cols++;
			$this->excel->getActiveSheet()->setCellValue('A' . $col, $num++);
			$this->excel->getActiveSheet()->setCellValue('B' . $col, $row['agama']);
		}

		//warna
		$cols2 = 3;
		foreach($list_data as $row){
			$col2 = $cols2++;
			$this->excel->getActiveSheet()->getStyle('A' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('B' . $col2)->applyFromArray($ContentStyle);
		}

		$this->excel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);


		$filename='data agama.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
		            
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}

	function pdf(){
		// rule type
		$this->rule->type('R');
		//ambil data
		$data['results'] = $this->m_master_agama->get_export();
		// pdf library
		$this->load->library('pdf');
		ob_start();		
		$this->load->view('back/layouts/master_agama/pdf', $data);
    $content = ob_get_clean();
  	try{
  		// L => Landscape, P => Potrait, array(215,330) => ukuran kertas, array(10, 10, 5, 5) => margin
      $html2pdf = new HTML2PDF('P', array(215,330), 'en', true, 'UTF-8', array(10, 10, 5, 5));
      $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
      $html2pdf->Output("data_agama.pdf");
  	}
  	catch(HTML2PDF_exception $e) {
      echo $e;
      exit;
  	}
	}

	function get_peserta() {
		$kd_peserta  	= $this->input->post('kd_peserta');
		$data 				= $this->m_surat_jaminan->get_peserta($kd_peserta);
		$jumlah 			= count($data);
		if($jumlah > 0){
			$arr_data = array(
				'nama'		=> $data['nama'],
				'alamat'	=> $data['alamat'],
				'ttl'			=> $data['tempat_lahir'].", ".id_date($data['tgl_lahir']),
			);
		}else{
			$arr_data = array(
				'nama'		=> "",
				'alamat'	=> "",
				'ttl'			=> "",
			);
		}
		echo json_encode($arr_data);
	}

}
